package com.feng.leetcode;

public class Eighth {
    public static void main(String[] args) {
        System.out.println(myAtoi("999999999999999999999"));
    }

    public static int myAtoi(String str) {
        char[] chars = str.toCharArray();
        StringBuilder s = new StringBuilder();
        boolean flag = true;
        for (int i = 0; i < chars.length; i++) {
            if (((chars[i] >='0' && chars[i]<='9') || chars[i] ==' ' || chars[i] == '-' || chars[i] == '+' || chars[i] == '\"' || chars[i] == '\'') && s.length() == 0){
                if ((chars[i] >='0' && chars[i]<='9')|| chars[i] == '+' || chars[i] == '-'){
                    s.append(chars[i]);
                }
            }else if (s.length() == 0){
                return 0;
            }else if (s.length() >0){
                if (chars[i] >='0' && chars[i]<='9'){
                    s.append(chars[i]);
                }else{
                    break;
                }
            }
        }
        try {
            if (s.length() ==0){
                return 0;
            }
            if (s.length() == 1 && (s.toString().charAt(0) == '-' || s.toString().charAt(0) == '+')){
                return 0;
            }
            return Integer.parseInt(s.toString());
        }catch (Exception e){
            e.printStackTrace();
            if (s.toString().charAt(0) == '-'){
                return Integer.MIN_VALUE;
            }else {
                return Integer.MAX_VALUE;
            }
        }
    }
}
